﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using Gift.WebApp;
using Gift.Model;

namespace WebApp.admin.goods
{
    public partial class attributes : BaseAdmin
    {
      
        tbAttributes oAttributes;
        tbAttributeValue oAttributeValue;

        string _attributeShow = "none";

        public string AttributeShow
        {
            get { return _attributeShow; }
        }

        public string OtherAttributeShow
        {
            get
            {
                if (_attributeShow == "none")
                {
                    return "block";
                }
                else
                {
                    return "none";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindGoodsType();
                this.BuildForm();
            }
        }

        void BindGoodsType()
        {
            DataTable dtGoodsType = BaseFactory.theGoodsType.GetGoodsTypeByCompanyId(int.Parse(base.Administrator.CompanyId));
            this.selectGoodsType.DataTextField = "typeName";
            this.selectGoodsType.DataValueField = "id";
            this.selectGoodsType.DataSource = dtGoodsType;
            this.selectGoodsType.DataBind();
            this.selectGoodsType.Items.Insert(0, new ListItem("请选择...", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                try
                {
                    oAttributes = BaseFactory.theAttributes.GetModel(int.Parse(Request.QueryString["id"]));
                    oAttributes.goodsType = int.Parse(this.selectGoodsType.SelectedItem.Value);
                    oAttributes.attributeName = this.txtAttributeName.Value;
                    oAttributes.attributeInputType = int.Parse(this.selectAttributeInputType.SelectedItem.Value);
                    oAttributes.createTime = DateTime.Now;
                    oAttributes.sortOrder = int.Parse(this.txtSortOrder.Value);
                    oAttributes.isRequired = int.Parse(this.rbListRequired.SelectedItem.Value);
                    oAttributes.state = this.rbListState.SelectedItem.Value == "0" ? false : true;
                 
                    BaseFactory.theAttributeValue.DeleteByAttribute(oAttributes.id.ToString());
                    if (BaseFactory.theAttributes.Update(oAttributes))
                    {
                        string[] arrValues;
                        
                        if(oAttributes.attributeInputType != 3 || oAttributes.attributeInputType != 4)
                        {
                            arrValues = Request.Form["otherAttributesValue"].Split(',');
                        }else
                            {
                                 arrValues = Request.Form["attributesValue"].Split(',');
                            }
                        foreach (string value in arrValues)
                        {
                            oAttributeValue = new tbAttributeValue();
                            oAttributeValue.attribute = oAttributes.id;
                            oAttributeValue.value = value;
                            BaseFactory.theAttributeValue.Add(oAttributeValue);
                        }

                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='attributesList.aspx';</script>");
                }
                catch
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }

            }
            else
            {
                try
                {
                    oAttributes = new tbAttributes();
                    oAttributes.goodsType = int.Parse(this.selectGoodsType.SelectedItem.Value);
                    oAttributes.attributeName = this.txtAttributeName.Value;
                    oAttributes.attributeInputType = int.Parse(this.selectAttributeInputType.SelectedItem.Value);
                    oAttributes.createTime = DateTime.Now;
                    oAttributes.sortOrder = int.Parse(this.txtSortOrder.Value);
                    oAttributes.isRequired = int.Parse(this.rbListRequired.SelectedItem.Value);
                    oAttributes.state = this.rbListState.SelectedItem.Value == "0" ? false : true;
                 
                    oAttributes.CompanyId = int.Parse(base.Administrator.CompanyId);
                    int id = BaseFactory.theAttributes.Add(oAttributes);
                    if (id > 0)
                    {
                        string[] arrValues = Request.Form["attributesValue"].Split(',');
                        foreach (string value in arrValues)
                        {
                            oAttributeValue = new tbAttributeValue();
                            oAttributeValue.attribute = id;
                            oAttributeValue.value = value;
                            BaseFactory.theAttributeValue.Add(oAttributeValue);
                        }

                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");window.location.href='attributesList.aspx';</script>");
                }
                catch
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
                
            }
        }

        void BuildForm()
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                oAttributes = BaseFactory.theAttributes.GetModel(int.Parse(base.Request.QueryString["id"]));
                this.txtAttributeName.Value = oAttributes.attributeName;
                this.selectGoodsType.Items.FindByValue(oAttributes.goodsType.ToString()).Selected = true;
                this.selectAttributeInputType.Items.FindByValue(oAttributes.attributeInputType.ToString()).Selected = true;
                this.txtSortOrder.Value = oAttributes.sortOrder.ToString();
                this.rbListRequired.Items.FindByValue(oAttributes.isRequired.ToString()).Selected = true;
                if (oAttributes.state == true)
                {
                    this.rbListState.Items.FindByValue("1").Selected = true;
                }
                else
                {
                    this.rbListState.Items.FindByValue("0").Selected = true;
                }

                DataTable dtValues = BaseFactory.theAttributeValue.GetAttributeValueByAttribute(oAttributes.id);
                if (oAttributes.attributeInputType == 3 || oAttributes.attributeInputType == 4)
                {
                    this._attributeShow = "block";
                 
                    foreach (DataRow dr in dtValues.Rows)
                    {
                        HtmlTableRow tRow = new HtmlTableRow();
                        HtmlTableCell tCell = new HtmlTableCell();
                        tCell.InnerHtml = "<input name=\"attributesValue\" type=\"text\" value=\"" + dr["value"].ToString() + "\" />";
                        tRow.Cells.Add(tCell);
                        HtmlTableCell tCel1 = new HtmlTableCell();
                        tCel1.InnerHtml = "<input type=button class=\"formButton\" value=\"删除\" title=\"删除属性值\" onclick=\"delSVInput(this.parentNode.parentNode.rowIndex);\">";
                        tRow.Cells.Add(tCel1);
                        this.attributesValues.Rows.Add(tRow);

                    }
                }
                else
                {
                    if (dtValues != null && dtValues.Rows.Count > 0)
                    {
                        this.otherAttributesValue.Value = dtValues.Rows[0]["value"].ToString();
                    }
                }
            }
        }
    }
}